package com.lin.auth.infra.basic.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.auth.infra.basic.entity.AuthUser;
import com.lin.auth.infra.basic.mapper.AuthUserDao;
import com.lin.auth.infra.basic.service.AuthUserService;
import com.mysql.cj.util.StringUtils;
import org.mapstruct.ap.shaded.freemarker.template.utility.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (AuthUser)表服务实现类
 *
 * @author makejava
 * @since 2023-11-01 01:25:23
 */
@Service("authUserService")
public class AuthUserServiceImpl extends ServiceImpl<AuthUserDao, AuthUser> implements AuthUserService {
    @Resource
    private AuthUserDao authUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AuthUser queryById(Long id) {
        return this.authUserDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param authUser 实例对象
     * @return 实例对象
     */
    @Override
    public Integer insert(AuthUser authUser) {
        return this.authUserDao.insert(authUser);
    }

    /**
     * 修改数据
     *
     * @param authUser 实例对象
     * @return 实例对象
     */
    @Override
    public AuthUser update(AuthUser authUser) {
        this.authUserDao.update(authUser);
        return this.queryById(authUser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.authUserDao.deleteById(id) > 0;
    }

    @Override
    public AuthUser findByFilter(AuthUser authUserBO) {
        return lambdaQuery()
                .eq(!StringUtils.isNullOrEmpty(authUserBO.getUserName()),
                        AuthUser::getUserName, authUserBO.getUserName())
                .one();
    }

    @Override
    public AuthUser findOneByFilter(AuthUser authUserBO) {
        return lambdaQuery()
                .eq(!StringUtils.isNullOrEmpty(authUserBO.getUserName()),
                        AuthUser::getUserName, authUserBO.getUserName())
                .one();
    }

    @Override
    public List<AuthUser> queryByCondition(AuthUser authUser) {
        return lambdaQuery()
                .eq(!StringUtils.isNullOrEmpty(authUser.getUserName()),
                        AuthUser::getUserName, authUser.getUserName())
                .eq(!Objects.isNull(authUser.getId()),
                        AuthUser::getId, authUser.getId())
                .list();
    }
}
